package com.zp.self.BIO;

import java.io.*;
import java.net.Socket;

/**
 * @author By ZengPeng
 * @Description
 * @date in  2021/3/31 19:07
 * @Modified By
 */
public class TimeServerHandler implements Runnable {
    private Socket socket;

    public TimeServerHandler(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        InputStream in = null;
        PrintWriter out = null;
        try {
            in = this.socket.getInputStream();
            out = new PrintWriter(this.socket.getOutputStream(), true);
            String body;
            while (true) {
                byte[] byteArray = new byte[4096];
                int read = in.read(byteArray, 0, 4096);
                if (read==-1)
                    break;
                body = new String(byteArray);
                System.out.println("this time server receive order :" + new String(byteArray));
                if (body.startsWith("QUERY TIME ORDER")) {
                    out.println(System.currentTimeMillis());
                } else {
                    out.println("BAD ORDER");//错误的命令
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
